/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wiflydemo_spotclient_ondesktop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 *
 * @author JaredSmith
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        //The listening port on the WiFly
        int port = 3000;

        byte[] buffer = new byte[3];
        int data;

        boolean running = true;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String temp = "";
        java.net.InetAddress address = java.net.InetAddress.getByName("169.254.1.1");
        DatagramSocket socket = new DatagramSocket();
        DatagramPacket out = null;

        //Display current IP settings for sending data
        System.out.println("### Sending to:");
        System.out.println("### ip: " + address); // IP address
        System.out.println("### port: " + port);

        System.out.println("Please enter a number from 0 to 255 in decimal.");
        System.out.println("Q or q will exit the program.");
        //Loop to read input until kill command is given
        while(running) {
            System.out.print("Entry:");
            //Clear buffer
            buffer = new byte[3];
            //Read the entry
            temp = br.readLine();
            //See if it is the kill command
            if(temp.equals("q") || temp.equals("Q")) {
                running = false;
            }
            //Else parse the string to an integer to check its value
            else {
                data = Integer.parseInt(temp);
                //Less than 0
                if(data < 0) {
                    System.out.println("Invalid Input >>> " + data + " >>> Please use number from 0 to 255");
                }
                //Greater than 255
                else if(data > 255) {
                    System.out.println("Invalid Input >>> " + data + " >>> Please use number from 0 to 255");
                }
                //Passed the limit checks so lets send the number out
                else {
                    if(temp.length() == 3) {
                        buffer[0] = (byte)temp.charAt(0);
                        buffer[1] = (byte)temp.charAt(1);
                        buffer[2] = (byte)temp.charAt(2);
                    }
                    else if(temp.length() == 2) {
                        buffer[0] = (byte)'0';
                        buffer[1] = (byte)temp.charAt(0);
                        buffer[2] = (byte)temp.charAt(1);
                    }
                    else {
                        buffer[0] = (byte)'0';
                        buffer[1] = (byte)'0';
                        buffer[2] = (byte)temp.charAt(0);
                    }
                    //Create a new datagram packet every time a new entry is given
                    out = new DatagramPacket(buffer, buffer.length, address, port);
                    //Send it out!
                    socket.send(out);
                    Thread.sleep(1500);
                }
            }
        }
    }

}
